var AddThis_Sharing = {

    shareCurrentPage: function() {
    	AddThis_Util.dbg("shareCurrentPage: sharing page...");
        var url = gBrowser.contentDocument.location.href;
        var title = gBrowser.contentTitle;
        AddThis_Sharing.invokeService('more', url, title);
    },
    
    sharePage: function(url, title) {
    	AddThis_Util.dbg("sharePage: sharing page...");
        AddThis_Sharing.invokeService('more', url, title);
    },
    
    invokeService: function(serviceId, url, title)
    {
    	AddThis_Util.dbg("invokeService: " + serviceId);
        
        if (typeof url == "undefined") url = gBrowser.contentDocument.location.href;
        if (typeof title == "undefined") title = gBrowser.contentTitle;
        
        
        
        if (url.indexOf(AddThis_Constants.OEXCHANGE_SERVICE)==0 ||
            url.indexOf('://')==-1) return;

        if (typeof serviceId == "string")
        {
            switch(serviceId)
            {
                case 'favorites':
                    if (gContextMenu && gContextMenu.onLink)
                    {
                        if (AddThis_Util.fxVersionAtLeast("3.0")) gContextMenu.bookmarkLink();
                        else BookmarksUtils.addBookmark(url,title);
                    }
                    else
                    {
                        var bkmkCommand = document.getElementById('Browser:AddBookmarkAs');
                        bkmkCommand.doCommand();
                    }
                    break;
                case 'mailto':
                    window.location.href = 'mailto:?subject='+
                        encodeURIComponent(title?title:'')+
                        '&body='+encodeURIComponent(url);
                    break; 
                case 'print':
                    gBrowser.contentWindow.print();
                    break;
                default:
                    var u = AddThis_Constants.OEXCHANGE_SERVICE;
                    u += (serviceId == 'more') ? "offer" : "forward/" + serviceId + "/offer";
                    u += "?url=" + encodeURIComponent(url) +
                         "&title=" + encodeURIComponent(title) +
                         "&pco=" + AddThis_Constants.PCO + "-" + AddThis_Constants.EXTENSION_VERSION;
                    var pubId = AddThis_Sharing._extractPubId();
                    if (pubId != false) u += "&username=" + encodeURIComponent(pubId);
                    
                    if (serviceId != 'more' && AddThis_Constants.services[serviceId].popup)
                    {
                        gBrowser.contentWindow.open(u, serviceId, 'scrollbars=no,menubar=no,width='+AddThis_Constants.services[serviceId].popup.width+',height='+AddThis_Constants.services[serviceId].popup.height+',resizable=yes,toolbar=no,location=no,status=no');
                    }
                    else
                    {
                        AddThis_Util.navToUrl(u);
                    }
                break;
            }
        }
        else
        {
            AddThis_Sharing.invokeService('more', url, title);
        }
    },
    
    initShareMenu: function(menuId,url,title)
    {
        var menupopup = document.getElementById(menuId);
        var sharethispageItem = document.getElementById(menuId+"_sharethispage");

        AddThis_Sharing.clearShareMenu(menupopup);
        
        var serviceId, newItem, items=0,
            activeServices = AddThis_Util.getActiveServicesObject();
        for (serviceId in activeServices)
        {
            newItem = AddThis_Sharing._createMenuitem(serviceId,url,title);
            if (!newItem) continue;
            menupopup.insertBefore(newItem,sharethispageItem);
            items++;
        }
        
        
        if (sharethispageItem._listener) sharethispageItem.removeEventListener("command", sharethispageItem._listener, false);
        sharethispageItem._listener = (typeof url == "undefined") ?
            this._shareCurrentPageHandler :
            AddThis_Util.tumeric(AddThis_Sharing._shareUrlHander,url,title);
        sharethispageItem.addEventListener("command", sharethispageItem._listener, false);
       
        if (items===0)
        {
            sharethispageItem.setAttribute("label", sharethispageItem.getAttribute("share_label"));
        }
        else
        {
            sharethispageItem.setAttribute("label", sharethispageItem.getAttribute("more_label"));
        }
        
    },
    
    clearShareMenu: function(menupopup)
    {
        var menuitems = menupopup.getElementsByTagName('menuitem');
        var item, removeItems = [];
        
        for(i=0; i<menuitems.length; i++)
        {
            item = menuitems[i];
            if (item.hasAttribute("class") && item.getAttribute("class").indexOf("addthis-service-item") !== -1) removeItems.push(item);
        }
        
        while(removeItems.length > 0) menupopup.removeChild(removeItems.pop());
    },
    
    _shareCurrentPageHandler: function(event)
    {
        AddThis_Sharing.shareCurrentPage();
        event.stopPropagation();
    },
    
    _shareUrlHander: function(url,title,event)
    {
        AddThis_Sharing.sharePage(AddThis_Util.slash(url),AddThis_Util.slash(title));
        event.stopPropagation();
    },
    
    _getObject: function()
    {
        if (typeof gBrowser.contentWindow.wrappedJSObject !== "undefined" &&
            typeof gBrowser.contentWindow.wrappedJSObject._ate == "object")
        {
            return gBrowser.contentWindow.wrappedJSObject._ate;
        }
        
        return false;
    },
    
    _extractPubId: function()
    {
        var obj = AddThis_Sharing._getObject();
        return (obj === false) ? false : obj.pub();
    },
    
    
    _createMenuitem: function(serviceId,url,title)
    {
        var newItem = document.createElement("menuitem");
        newItem.setAttribute("serviceid",serviceId);
        newItem.setAttribute("label",AddThis_Constants.services[serviceId].title);
        newItem.setAttribute("tooltiptext",AddThis_Constants.services[serviceId].title);
        newItem.setAttribute("class","menuitem-iconic addthis-service-item");
        newItem.setAttribute("style","list-style-image: url('chrome://addthis/skin/services/"+serviceId+".gif');");
        if (url !== undefined)
        {
            
            if (AddThis_Constants.services[serviceId].currentOnly) return false;
            newItem.setAttribute("oncommand", "AddThis_Sharing.invokeService('"+serviceId+"','"+AddThis_Util.slash(url)+"','"+AddThis_Util.slash(title)+"');event.stopPropagation();");
        }
        else
        {
            newItem.setAttribute("oncommand", "AddThis_Sharing.invokeService('"+serviceId+"');event.stopPropagation();");
        }
        return newItem;
    }

};